package com.xjyandkj.socialcalendar.ui.calendar;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.xjyandkj.socialcalendar.R;
import com.xjyandkj.socialcalendar.adapter.calendar.CalendarAdapter;

public class CalendarActivity extends Activity{
	Calendar cal = Calendar.getInstance();
	int currentYear = cal.get(Calendar.YEAR);
	int currentMonth = cal.get(Calendar.MONTH);
	int currentDay = cal.get(Calendar.DAY_OF_MONTH);
	TextView yearmonth;  
    Button nextMonthBtn,preMonthBtn;  
    GridView gridView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calendar_calendar);
		
		gridView = (GridView) findViewById(R.id.gridview);
		CalendarAdapter calendarAdapter = new CalendarAdapter(this,currentYear,currentMonth,currentDay);
		gridView.setAdapter(calendarAdapter);
		
		yearmonth = (TextView) findViewById(R.id.yearmonth);
		yearmonth.setText(currentYear + "年" + (currentMonth+1) + "月");
		
		nextMonthBtn = (Button) findViewById(R.id.nextMonth);
		preMonthBtn = (Button) findViewById(R.id.preMonth);
		
		nextMonthBtn.setOnClickListener(new NextMonthBtnListener());
		preMonthBtn.setOnClickListener(new PreMonthBtnListener());
		
	}
	
	//显示下一个月的信息  
    private class NextMonthBtnListener implements OnClickListener{  
  
        public void onClick(View v) {  
            if(currentMonth == 11)  
            {  
                currentMonth = 0;  
                currentYear++;  
            }else{  
                currentMonth++;  
            }  
            gridView.setAdapter(new CalendarAdapter(CalendarActivity.this,currentYear,currentMonth,currentDay));  
            updataDataView(currentYear,currentMonth);  
        }  
          
    }  
    //显示前一个月的信息  
    private class PreMonthBtnListener implements OnClickListener{  
  
        public void onClick(View v) {  
            if(currentMonth == 0)  
            {  
                currentYear--;  
                currentMonth = 11;  
            }else{  
                currentMonth--;  
            }  
            gridView.setAdapter(new CalendarAdapter(CalendarActivity.this,currentYear,currentMonth,currentDay));  
            updataDataView(currentYear,currentMonth);  
        }  
          
    }  
    //刷新标题日期  
    private void updataDataView(int year, int month){  
        month++;  
        yearmonth.setText(year+"年"+month+"月");  
  
    }  
}